﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FrmMain : Form
    {
        PictureBox[] PicBoxes = new PictureBox[10];
        Label[] LblDays = new Label[10];
        Label[] LblDates = new Label[10];
        Label[] LblForecasts = new Label[10];
        Microsoft.VisualBasic.PowerPacks.LineShape[] Lines = new Microsoft.VisualBasic.PowerPacks.LineShape[8];

        public FrmMain()
        {
            InitializeComponent();

            // Initialize window settings

                // TxtCity textbox
                TxtCity.Text = "Enter city name to search for...";
                TxtCity.ForeColor = Color.Gray;
                TxtCity.Font = new Font(TxtCity.Font, TxtCity.Font.Style | FontStyle.Italic);
        }

        private void BtnSearch_Click(object sender, EventArgs e)
        {
            // Reinitialize screen
            GrpForecast.Visible = false;
            GrpSelection.Visible = false;

            // Select type of display based on result
            switch (weatherforecaster.ClsWunderground.ParseResult(TxtCity.Text))
            { 
                case "Not Found":       // No results found -> pop-up a message box
                    MessageBox.Show("No results found.\nPlease check your query and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;

                case "Ambiguous":       // Ambiguous results found -> display options for selection
                    List<weatherforecaster.ClsWunderground.ListOfCityStateCountry> RsltsAmbgs = weatherforecaster.ClsWunderground.RsltsCtySttCty;
                    LstSelection.Items.Clear();

                    for (int i = 0; i < RsltsAmbgs.Count(); i++)
                    {
                        string tmp = "";
                        tmp = RsltsAmbgs[i].City;
                        if (RsltsAmbgs[i].State != null && RsltsAmbgs[i].State != "") tmp += ", " + RsltsAmbgs[i].State;
                        if (RsltsAmbgs[i].Country != null && RsltsAmbgs[i].Country != null) tmp += ", " + RsltsAmbgs[i].Country;
                        LstSelection.Items.Add(tmp);
                    }
                    RsltsAmbgs = null;
                    GrpSelection.Visible = true;
                    break;

                case "Forecast":        // Forecast found -> display forcast.
                    List<weatherforecaster.ClsWunderground.Forecast> RsltsFrcst = weatherforecaster.ClsWunderground.RsltsFrcst;

                    for (int i = 0; i < 10; i++)
                    {
                        PicBoxes[i].ImageLocation = RsltsFrcst[i].ConditionIcon;
                        LblDays[i].Text = RsltsFrcst[i].Day;
                        LblDates[i].Text = RsltsFrcst[i].Month + "\n" + RsltsFrcst[i].DD;
                        LblForecasts[i].Text = "Conditions: " + RsltsFrcst[i].Condition + "\n" +
                            "High: " + RsltsFrcst[i].HighF + "°F / " + RsltsFrcst[i].HighC + "°C" + "\n" +
                            "Low: " + RsltsFrcst[i].LowF + "°F / " + RsltsFrcst[i].LowC + "°C";
                    }

                    GrpForecast.Visible = true;
                    RsltsFrcst = null; 
                    break;
                default:
                    break;
            }
        }

        private void TxtCity_TextChanged(object sender, EventArgs e)
        {
            // Reverts back to regular font style upon text change
            if (TxtCity.ForeColor != Color.FromName("WindowText"))
            {
                TxtCity.ForeColor = Color.FromName("WindowText");
                TxtCity.Font = new Font(TxtCity.Font, TxtCity.Font.Style & FontStyle.Regular);
            }
        }

        private void TxtCity_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) BtnSearch_Click(TxtCity, null);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            #region EnumerateObjects
            PicBoxes[0] = this.PicBox1;
            PicBoxes[1] = this.PicBox2;
            PicBoxes[2] = this.PicBox3;
            PicBoxes[3] = this.PicBox4;
            PicBoxes[4] = this.PicBox5;
            PicBoxes[5] = this.PicBox6;
            PicBoxes[6] = this.PicBox7;
            PicBoxes[7] = this.PicBox8;
            PicBoxes[8] = this.PicBox9;
            PicBoxes[9] = this.PicBox10;

            LblDays[0] = this.LblDay1;
            LblDays[1] = this.LblDay2;
            LblDays[2] = this.LblDay3;
            LblDays[3] = this.LblDay4;
            LblDays[4] = this.LblDay5;
            LblDays[5] = this.LblDay6;
            LblDays[6] = this.LblDay7;
            LblDays[7] = this.LblDay8;
            LblDays[8] = this.LblDay9;
            LblDays[9] = this.LblDay10;

            LblDates[0] = this.LblDate1;
            LblDates[1] = this.LblDate2;
            LblDates[2] = this.LblDate3;
            LblDates[3] = this.LblDate4;
            LblDates[4] = this.LblDate5;
            LblDates[5] = this.LblDate6;
            LblDates[6] = this.LblDate7;
            LblDates[7] = this.LblDate8;
            LblDates[8] = this.LblDate9;
            LblDates[9] = this.LblDate10;

            LblForecasts[0] = this.LblForcast1;
            LblForecasts[1] = this.LblForcast2;
            LblForecasts[2] = this.LblForcast3;
            LblForecasts[3] = this.LblForcast4;
            LblForecasts[4] = this.LblForcast5;
            LblForecasts[5] = this.LblForcast6;
            LblForecasts[6] = this.LblForcast7;
            LblForecasts[7] = this.LblForcast8;
            LblForecasts[8] = this.LblForcast9;
            LblForecasts[9] = this.LblForcast10;

            Lines[0] = this.Line1;
            Lines[1] = this.Line2;
            Lines[2] = this.Line3;
            Lines[3] = this.Line4;
            Lines[4] = this.Line5;
            Lines[5] = this.Line6;
            Lines[6] = this.Line7;
            Lines[7] = this.Line8;

            #endregion

            switch (weatherforecaster.Properties.Settings.Default.ForcastDays)
            {
                case 7:
                    for (int i = 7; i < 10; i++)
                    {
                        PicBoxes[i].Visible = false;
                        LblDays[i].Visible = false;
                        LblDates[i].Visible = false;
                        LblForecasts[i].Visible = false;
                    }
                    for (int i = 5; i < 8; i++)
                    {
                        Lines[i].Visible = false;
                    }
                    break;
                case 10:
                default:
                    for (int i = 0; i < 10; i++)
                    {
                        PicBoxes[i].Visible = true;
                        LblDays[i].Visible = true;
                        LblDates[i].Visible = true;
                        LblForecasts[i].Visible = true;
                    }
                    for (int i = 0; i < 8; i++)
                    {
                        Lines[i].Visible = true;
                    }
                    break;
            }

        }

        private void LstSelection_MouseClick(object sender, MouseEventArgs e)
        {
            TxtCity.Text = LstSelection.SelectedItem.ToString();
        }

        private void LstSelection_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TxtCity.Text = LstSelection.SelectedItem.ToString();
            BtnSearch_Click (LstSelection,null);
        }

    }
}
